package com.zyd.module.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zyd.common.util.CoreConst;
import com.zyd.common.util.ResultUtil;
import com.zyd.enums.GlobalConstant;
import com.zyd.module.admin.model.BizLink;
import com.zyd.module.admin.vo.base.PageResultVo;
import com.zyd.module.admin.vo.base.ResponseVo;
import com.zyd.module.api.vo.CustomerVo;
import javafx.scene.input.DataFormat;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;


import com.zyd.module.admin.model.CustomerEntity;
import com.zyd.module.admin.service.CustomerService;

import javax.servlet.http.HttpServletRequest;


/**
 * 
 *
 * @author 铁柱
 * @email *****@mail.com
 * @date 2022-04-04 00:17:38
 */
@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    /**
    * 跳转到页面
    */
    @ApiOperation(value = "客户列表")
    @GetMapping("/list")
    public String customer() {
        return CoreConst.ADMIN_PREFIX + "customer/list";
    }

    @PostMapping("/table")
    @ResponseBody
    public PageResultVo loadLinks(CustomerVo customerVo, Integer pageNumber, Integer pageSize) {
        IPage<CustomerEntity> customerPages = customerService.pageCustomerList(customerVo, pageNumber, pageSize);
        return ResultUtil.table(customerPages.getRecords(), customerPages.getTotal());
    }

    @ApiOperation(value = "查看客户详细信息")
    @GetMapping("/look")
    public String add(HttpServletRequest request,String cid, Model model){
        CustomerEntity customerEntity = customerService.getById(cid);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 EEEE HH时mm分ss秒", Locale.CHINA);
        String timeCustomer = simpleDateFormat.format(customerEntity.getCreateTime());
        String[] proNames = customerEntity.getProName().split(",");
        List<String> proList=Arrays.asList(proNames);
        model.addAttribute("products",proList);
        model.addAttribute("time",timeCustomer);
        model.addAttribute("customer",customerEntity);
        return CoreConst.ADMIN_PREFIX + "customer/infos";
    }

    @ApiOperation(value = "删除")
    @PostMapping("/delete")
    @ResponseBody
    public ResponseVo deleteCustomer(String id) {
        CustomerEntity entity =new  CustomerEntity(id,GlobalConstant.CUSTOMER_DEL);
        if (entity==null) {
            return ResultUtil.error("客户信息不存在！");
        }
        boolean update = customerService.updateById(entity);
        if (update) {
            return ResultUtil.success("删除数据成功");
        } else {
            return ResultUtil.error("删除数据失败");
        }
    }
    @ApiOperation(value = "恢复所有客户信息")
    @ResponseBody
    @PostMapping("/restore")
    public ResponseVo restoreData(Integer status){
        if (status==200){
            UpdateWrapper<CustomerEntity> updateWrapper=new UpdateWrapper<>();
            updateWrapper.set("del_flag",GlobalConstant.CUSTOMER_TRUE);
            boolean update = customerService.update(updateWrapper);
            if (update) {
                return ResultUtil.success("恢复客户信息成功");
            } else {
                return ResultUtil.error("恢复客户信息失败");
            }
        }else {
            return ResultUtil.error("恢复客户信息失败");
        }
    }

    @ApiOperation(value = "批量删除")
    @PostMapping("/batch/delete")
    @ResponseBody
    public ResponseVo deleteBatch(@RequestParam("ids[]") String[] ids) {
        Integer count = customerService.removeCustomerByIds(Arrays.asList(ids));
        if (count==ids.length) {
            return ResultUtil.success("批量删除客户信息成功！");
        } else if (count>0&&count<ids.length){
            return ResultUtil.success("共删除了"+count+"条数据，未删除客户信息可能存在问题！");
        } else{
            return ResultUtil.error("批量删除客户信息失败");
        }
    }


}
